package com.wedu.service.impl;

import com.wedu.DAO.UserDao;
import com.wedu.DAO.impl.UserDaoImpl;
import com.wedu.entity.Job;
import com.wedu.entity.User;
import com.wedu.service.UserService;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

public class UserServiceImpl implements UserService {
    private UserDao userDao= new UserDaoImpl();

    @Override
    public boolean name_t(String uid) {
        User user = userDao.staff(uid);
        return user != null;
    }

    @Override
    public boolean name_t(String uid,String name){
        User user = userDao.staff(uid);
        return user != null && user.getName().equals(name);
    }

    @Override
    public boolean uid_t(String uid,String name) {
        User user=userDao.school_staff(uid);
        return user != null && user.getName().equals(name);
    }

    @Override
    public boolean uid_t(String uid) {
        User user=userDao.school_staff(uid);
        return user != null ;
    }

    @Override
    public boolean acid(String acid) {
        User user=userDao.acid_1(acid);
        return user != null;
    }

    @Override
    public boolean addUser(User user) {
        int i=userDao.add_user(user);
        return i != 0;
    }

    @Override
    public boolean password(String id, String password) {
        int i = userDao.update_user(id, password);
        return i != 0;
    }

    @Override
    public User login_t(String id, String password) {
        User user=userDao.staff(id);
        if (user.getIndex()>0){
            return user;
        }
        System.out.println("你连续5次登录密码错误，请找回密码！");
        return null;
    }

    @Override
    public void login_index(int index,String id) {
        userDao.update_index(index,id);
    }

    @Override
    public boolean modify(User user) {
        int i = userDao.update_user(user);
        return i > 0;
    }

    @Override
    public int level(User user) {
        User user1 = userDao.staff(user.getACID());
        return user1.getCredit_rating();
    }

    @Override
    public double balance(User user) {
        User user1 = userDao.staff(user.getACID());
        return user1.getBalance();
    }

    @Override
    public int frequency(User user) {
        User user1 = userDao.staff(user.getACID());
        return user1.getFrequency();
    }

    @Override
    public int frequency_t(User user) {
        User user1 = userDao.staff(user.getACID());
        return user1.getFrequency_t();
    }

    @Override
    public User id(String id) {
        return userDao.staff(id);
    }

    @Override
    public boolean level(String id,int level) {
        int update_level = userDao.update_level(id, level);
        return update_level != 0;
    }

    @Override
    public void acid_remove(String acid) {
        userDao.remove_user(acid);
    }

    @Override
    public boolean add_personnel(String id, String name) {
        return userDao.addPersonnel(id, name)!=0;
    }

    @Override
    public List<User> information() {
        return userDao.user();
    }

    @Override
    public void information(Job job) {
        userDao.add_information(job);
    }

    @Override
    public List<Job> job_information(String acid) {
        return userDao.job(acid);
    }

    @Override
    public void revoke(int did) {
        userDao.revoke_information(did);
    }

    @Override
    public List<String> job_acid(int did) {
        return userDao.acid(did);
    }

    @Override
    public void user_balance(String acid,double salary) {
        userDao.update_user_balance(acid,salary);
    }

    @Override
    public Job information(int did,String acid) {
        Job job = userDao.information(did,acid);
        return job;
    }

    @Override
    public List<Job> look() {
        return userDao.information();
    }

    @Override
    public boolean handle_did(int did,int in) {
        Job job = userDao.information(did);
        if (job==null)
            return false;
        return job.getApproval_status() == in;
    }

    @Override
    public Job didG(int did) {
        return userDao.information(did);
    }

    @Override
    public void handle(int did, int status) {
        userDao.handleG(did,status);
    }

    @Override
    public List<Job> look_over() {
        return userDao.looks_over();
    }

    @Override
    public void job_level(int did, int level) {
        userDao.update_job_level(did,level);
    }

    @Override
    public void jobType_name(int did,String jobType_name) {
        userDao.update_jobType_name(did,jobType_name);
    }

    @Override
    public void deduct_salary(int did,double deduct_salary) {
        userDao.update_deduct_salary(did,deduct_salary);
    }

    @Override
    public List<Job> completedG() {
        return userDao.completed();
    }

    @Override
    public List<Job> overdue() {
        List<Job> jobs = userDao.looks_over();
        if (jobs==null)
            return null;
        List<Job> jobList=new ArrayList<Job>();
        for (Job job:jobs){
            //获取当前时间毫秒值
            long time=System.currentTimeMillis();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd HH：mm");
            //获取兼职结束时间
            long time1=0;
            try {
                time1=sdf.parse(job.getJob1_time()).getTime();
            }catch (Exception e){
                e.fillInStackTrace();
            }
            time=time1/1000/60/60-time/1000/60/60;
            if (job.getStare().equals("true")&&time<0){
                jobList.add(job);
            }
        }
        return jobList;
    }

    @Override
    public List<Job> position_information() {
        List<Job> jobs = userDao.looks_over();
        if (jobs==null)
            return null;
        List<Job> jobList=new ArrayList<Job>();
        for (Job job:jobs){
            //获取当前时间毫秒值
            long time=System.currentTimeMillis();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd HH：mm");
            //获取兼职结束时间
            long time1=0;
            try {
                time1=sdf.parse(job.getJob1_time()).getTime();
            }catch (Exception e){
                e.fillInStackTrace();
            }
            time=time1/1000/60/60-time/1000/60/60;
            if (time>0)
                jobList.add(job);
        }
        return jobList;
    }

    @Override
    public boolean repeat(String job_acid, int did) {
        List<Job> jobs = userDao.worker();
        for (Job job:jobs){
            if (job.getDID()==did&&job.getJob_acid().equals(job_acid)){
                return true;
            }
        }
        return false;
    }

    @Override
    public List<Job> processing(String job_acid) {
        List<Job> jobs = userDao.worker();
        if (jobs==null)
            return null;
        List<Job> jobList=new ArrayList<Job>();
        for (Job job:jobs){
            if (job.getJob_acid().equals(job_acid)&&job.getWork_stare()==1)
                jobList.add(job);
        }
        return jobList;
    }

    @Override
    public void add_worker(Job job) {
        userDao.addWorker(job);
    }

    @Override
    public List<Job> finish(String job_acid) {
        List<Job> jobs = userDao.worker();
        if (jobs==null)
            return null;
        List<Job> jobList=new ArrayList<Job>();
        for (Job job:jobs){
            if (job.getJob_acid().equals(job_acid)&&job.getWork_stare()==0)
                jobList.add(job);
        }
        return jobList;
    }

    @Override
    public void update_end(int did) {
        userDao.update_work_stare(did);
    }

    @Override
    public void update(int did) {
        userDao.update_stare(did);
    }
}
